package com.yilin.tms.order.service.statistics.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.tms.core.entity.data.statistics.order.goods.GoodsStatistics;
import com.yilin.tms.core.entity.data.statistics.order.rob.RobStatistics;
import com.yilin.tms.core.entity.data.statistics.order.transit.TransitStatistics;
import com.yilin.tms.core.entity.order.clearing.ClearingDetail;
import com.yilin.tms.core.entity.order.goods.GoodsOrder;
import com.yilin.tms.core.entity.order.payment.OrderPaymentCost;
import com.yilin.tms.core.entity.order.payment.OrderRefundCost;
import com.yilin.tms.core.entity.order.payment.OrderSettlementCost;
import com.yilin.tms.core.entity.order.rob.RobOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.order.mapper.clearing.ClearingDetailMapper;
import com.yilin.tms.order.mapper.payment.OrderPaymentCostMapper;
import com.yilin.tms.order.mapper.payment.OrderRefundCostMapper;
import com.yilin.tms.order.mapper.payment.OrderSettlementCostMapper;
import com.yilin.tms.order.mapper.rob.RobOrderMapper;
import com.yilin.tms.order.mapper.statistics.OrderStatisticsMapper;
import com.yilin.tms.order.mapper.transit.TransitOrderMapper;
import com.yilin.tms.order.service.statistics.IOrderStatisticsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackOn = Exception.class)
public class OrderStatisticsService extends ServiceImpl<RobOrderMapper, RobOrder> implements IOrderStatisticsService {

    @Resource
    OrderStatisticsMapper statisticsMapper;
    @Resource
    ClearingDetailMapper clearingDetailMapper;
    @Resource
    TransitOrderMapper transitOrderMapper;
    @Resource
    OrderPaymentCostMapper orderPaymentCostMapper;
    @Resource
    OrderRefundCostMapper orderRefundCostMapper;
    @Resource
    OrderSettlementCostMapper orderSettlementCostMapper;

    /**
     * 获取货源统计数据
     */
    @Override
    public GoodsStatistics getGoodsStatistics(Account loginUser) {
        //货源总计,本年、本月、今日计数
        GoodsStatistics goodsStatistics = statisticsMapper.getGoodsCount(loginUser);
        //货源状态分布饼图
        goodsStatistics.setGoodsStatusCake(statisticsMapper.getGoodsStatusCake(loginUser));
        //货源计价模式分布饼图
        goodsStatistics.setGoodsPriceModeCake(statisticsMapper.getGoodsPriceModeCake(loginUser));
        //货源结算方式分布饼图
        goodsStatistics.setGoodsSettlementTypeCake(statisticsMapper.getGoodsSettlementTypeCake(loginUser));
        //货源类型分布饼图
        goodsStatistics.setGoodsTypeCake(statisticsMapper.getGoodsTypeCake(loginUser));
        //新增货源趋势折线图
        goodsStatistics.setGoodsAddTrends(statisticsMapper.getGoodsAddTrends(loginUser));
        return goodsStatistics;
    }

    /**
     * 获取抢单统计数据
     */
    @Override
    public RobStatistics getRobStatistics(Account loginUser) {
        //抢单总计,本年、本月、今日计数
        RobStatistics robStatistics = statisticsMapper.getRobCount(loginUser);
        //抢单状态分布饼图
        robStatistics.setRobStatusCake(statisticsMapper.getRobStatusCake(loginUser));
        //抢单类型分布饼图
        robStatistics.setRobTypeCake(statisticsMapper.getRobTypeCake(loginUser));
        //新增抢单趋势折线图
        robStatistics.setRobAddTrends(statisticsMapper.getRobAddTrends(loginUser));
        return robStatistics;
    }

    /**
     * 获取运单统计数据
     */
    @Override
    public TransitStatistics getTransitStatistics(Account loginUser) {
        //运单总计,本年、本月、今日计数
        TransitStatistics transitStatistics = statisticsMapper.getTransitCount(loginUser);
        //运单状态分布饼图
        transitStatistics.setTransitStatusCake(statisticsMapper.getTransitStatusCake(loginUser));
        //运单省运管状态饼图
        transitStatistics.setTransitTmsStatusCake(statisticsMapper.getTransitTmsStatusCake(loginUser));
        //新增运单趋势折线图
        transitStatistics.setTransitAddTrends(statisticsMapper.getTransitAddTrends(loginUser));
        return transitStatistics;
    }

    @Override
    public Integer getClearingCount() {
        QueryWrapper<ClearingDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clearing_status", ClearingDetail.ClearingStatus.going);
        return Math.toIntExact(clearingDetailMapper.selectCount(queryWrapper));
    }

    @Override
    public Integer getGatherRecordCount() {
        QueryWrapper<TransitOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("transit_status", TransitOrder.TransitStatus.waitingSettlement);
        queryWrapper.eq("price_mode", GoodsOrder.PriceMode.notPrice);
        queryWrapper.eq("gather_transport_cost", 0);
        return Math.toIntExact(transitOrderMapper.selectCount(queryWrapper));
    }

    @Override
    public Integer getOrderPaymentCostCount() {
        QueryWrapper<OrderPaymentCost> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_status", OrderPaymentCost.PayStatus.create);
        return Math.toIntExact(orderPaymentCostMapper.selectCount(queryWrapper));
    }

    @Override
    public Integer getOrderRefundCostCount() {
        QueryWrapper<OrderRefundCost> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("refund_status", OrderRefundCost.RefundStatus.approved);
        return Math.toIntExact(orderRefundCostMapper.selectCount(queryWrapper));
    }

    @Override
    public Integer getOrderSettlementCostCount() {
        QueryWrapper<OrderSettlementCost> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_status", OrderSettlementCost.PayStatus.approved);
        return Math.toIntExact(orderSettlementCostMapper.selectCount(queryWrapper));
    }

    @Override
    public List<Map<String, Object>> getTradingTrendData(String organizeId) {
        return transitOrderMapper.getTradingTrendData(organizeId);
    }
}
